<?php
/**
 * created by zzy
 * date: 2018/1/6 15:49
 */
namespace App\Eloquent\Ygt;
use DB;
class OrderField extends DbEloquent{

    protected $table    = 'ygt_order_field';

    //取厂的工单第一步字段列表
    public static function getCompanyFieldList( $companyId, $where=[] ){

        self::setFieldList($companyId);
        $where['company_id']       = $companyId;
        $obj                        = OrderFieldCompany::getObject();
        $collection                 = OrderFieldCompany::setWhere($obj,$where);
        $collection                 = $collection->orderby('status','desc');
        $collection                 = $collection->orderby('orderby','asc');
        $collection                 = $collection->orderby('field_id','asc');
        $collection                 = $collection->get();
        $lists                      = $collection->toArray();
        $result                     = [];
        foreach( $lists as $key=>$val )
        {
            $arr                    = $val;
            $arr['id']              = $val['field_id'];
            $result[]               = $arr;
        }
        return $result;
    }
    //设置厂的工单第一步字段
    public static function setFieldList($companyId)
    {
        $where['company_id']    = $companyId;
        $collection               = OrderFieldCompany::getList($where);
        $lists                    = $collection->toArray();
        $fieldIdArr               = [];
        foreach( $lists as $key=>$val )
        {
            $fieldIdArr[]         = $val['field_id'];
        }
        $result                  = true;
        $where                   = [];
        if( $fieldIdArr ){
            $where['id']        = ['notIn',$fieldIdArr];
        }
        $fieldList               = self::getList($where)->toArray();
        if( $fieldList ){
            $data               = $copyFieldIdArr = [];
            foreach( $fieldList as $key=>$val)
            {
                $valId                   = $val['id'];
                $arr                     = $val;
                unset($arr['id']);
                $arr['company_id']      = $companyId;
                $arr['field_id']        = $valId;
                $data[]                  = $arr;
                $copyFieldIdArr[]        = $valId;
            }
            $result             = DB::table('ygt_order_field_company')->insert($data);
            if( $companyId != 1 )
            {
                self::copyCompanyFieldSelect($companyId,$copyFieldIdArr);
            }
        }
        return $result;
    }
    //复制字段的对应的下拉值
    public static function copyCompanyFieldSelect($companyId,$fieldIdArr)
    {
        $where              = ['company_id'=>1,'field_id'=>['in',$fieldIdArr]];
        $colletion          = OrderFieldCompanySelect::getList($where);
        $lists              = $colletion->toArray();
        $nowTime            = $_SERVER['REQUEST_TIME'];
        $data               = $pidArr = [];
        foreach( $lists as $key=>$val )
        {
            $valId                    = $val['id'];
            $valPid                   = $val['pid'];
            if( $valPid == 0 ){
                $arr                      = $val;
                unset($arr['id']);
                $arr['copy_id']         = $valId;
                $arr['company_id']      = $companyId;
                $arr['created_at']      = $nowTime;
                $arr['updated_at']      = $nowTime;
                $data[]                  = $arr;
            } else {
                $pidArr[]               = $valPid;
            }
        }
        $table              = 'ygt_order_field_company_select';
        $result             = DB::table($table)->insert($data);
        if($pidArr)
        {
            $where              = ['company_id'=>$companyId,'copy_id'=>['in',$pidArr]];
            $copyColletion      = OrderFieldCompanySelect::getList($where);
            $copyIdAndPid       = $copyColletion->pluck('id','copy_id')->toArray();
            //var_dump($copyIdAndPid);exit;
            $newData            = [];
            foreach( $lists as $key=>$val )
            {
                $valId                    = $val['id'];
                $valPid                   = $val['pid'];
                if( $valPid != 0 ){
                    $newPid                   = $copyIdAndPid[$valPid];
                    $arr                      = $val;
                    unset($arr['id']);
                    $arr['copy_id']         = $valId;
                    $arr['pid']              = $newPid;
                    $arr['company_id']      = $companyId;
                    $arr['created_at']      = $nowTime;
                    $arr['updated_at']      = $nowTime;
                    $newData[]                = $arr;
                }
            }
            $table              = 'ygt_order_field_company_select';
            $result             = DB::table($table)->insert($newData);
        }
        return $result;
    }
    //修改厂的工单第一步字段列表
    public static function editCompanyField($companyId,$params)
    {
        $fieldId            = isset($params['id']) ? $params['id'] : 0;
        $title              = $params['title'];
        $placeholder        = $params['placeholder'];
        $fieldType          = $params['field_type'];
        $textType           = $params['text_type'];
        $orderby            = $params['orderby'] ? $params['orderby'] : 0;
        $is_must            = $params['is_must'];
        $status             = $params['status'];
        $data               = [
            'title'=>$title,'placeholder'=>$placeholder,
            'field_type'=>$fieldType,'text_type'=>$textType,
            'orderby'=>$orderby,'is_must'=>$is_must,
            'status'=>$status
        ];
        $result                     = false;
        if( $companyId == 1){
            if($fieldId){
                $where                  = ['title'=>$title,'id'=>['!=',$fieldId]];
                $isExist               = self::getInfo( $where );
                if( $isExist ){
                    $result             = 'exist';
                } else {
                    //修改主表
                    $where              = ['id'=>$fieldId];
                    $updateResult       = self::updateOneData( $where, $data );
                    //同时修改厂工单第一步
                    $where              = ['company_id'=>$companyId,'field_id'=>$fieldId];
                    OrderFieldCompany::updateOneData( $where, $data );
                    $result             = $updateResult ? true : false;
                }
            } else {
                // 添加字段
                $where                  = ['title'=>$title];
                $isExist                = self::getInfo( $where );
                if( $isExist ){
                    $result            = 'exist';
                } else {
                    $nowTime           = $_SERVER['REQUEST_TIME'];
                    $fieldName         = 'a'.$nowTime;
                    $data['field_name']   = $fieldName;
                    $insert_id          = self::insertOneData( $data,'id' );
                    $result             = false;
                    if( $insert_id )
                    {
                        $where          = ['id'=>$insert_id];
                        $fieldName     = 'field_name_'.$insert_id;
                        $data           = [ 'field_name'=>$fieldName ];
                        $result         = self::updateOneData( $where, $data );
                        $sql            = "ALTER TABLE `ygt_order` ADD COLUMN `".$fieldName."` VARCHAR(100) DEFAULT '' NOT NULL";
                        $sql2           = "ALTER TABLE `ygt_order_pre` ADD COLUMN `".$fieldName."` VARCHAR(100) DEFAULT '' NOT NULL";
                        $sql3           = "ALTER TABLE `ygt_customer_order` ADD COLUMN `".$fieldName."` VARCHAR(100) DEFAULT '' NOT NULL";
                        $sql4           = "ALTER TABLE `ygt_customer_order_pre` ADD COLUMN `".$fieldName."` VARCHAR(100) DEFAULT '' NOT NULL";
                        DB::statement($sql);
                        DB::statement($sql2);
                        DB::statement($sql3);
                        DB::statement($sql4);
                    }
                }
            }
        } else {
            //更新厂的工单第一步字段
            if($fieldId){
                $where                  = [
                    'company_id'=>$companyId,'title'=>$title,
                    'field_id'=>['!=',$fieldId]
                ];
                $isExist               = OrderFieldCompany::getInfo( $where );
                if( $isExist ){
                    $result             = 'exist';
                } else {
                    $where              = [ 'company_id'=>$companyId,'field_id'=>$fieldId ];
                    $result             = OrderFieldCompany::updateOneData( $where, $data );
                }
            }
        }
        return $result;
    }
}
